package java_thinking.unit_15._15_10;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description :   使用Number和Integer证明数组的协变性;
 * @author: cww
 * @DateTime: 2019-04-05 23:30
 */

public class E26 {
    public static void main(String[] args) {
        Number[] numbers = new Integer[3];
        numbers[0] = new Integer(0);
        numbers[1] = new Integer(1);
        numbers[2] = new Integer(2);
        try {
            numbers[1] = new Double(3.4);
        } catch (Exception e) {
            System.out.println(e);
        }
        for (Number number: numbers){
            System.out.println(numbers);
        }
        for (Number number:numbers){
            System.out.println(number.getClass().getSimpleName());
        }

        /**
         * 证明协变性对list不起作用
         *
         */
        List<? extends Number> nlist = new ArrayList<Integer>();
        // Compile Error: can't add any type of object:
        // nlist.add(new Integer(1));
        // nlist.add(new Float(1.0));
        // nlist.add(new Object());
        nlist.add(null); // Legal but uninteresting
        // We know that it returns at least Number:
        Number n = nlist.get(0);
    }

}
